package com.sservice.commonBusiness.service.impl;

import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.mapper.BusinessApplyMapper;
import com.sservice.commonBusiness.e.BusinessApply;
import com.sservice.commonBusiness.service.BusinessApplyService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/02/26.
 */
@Service
public class BusinessApplyServiceImpl implements BusinessApplyService {

    @Autowired
    private BusinessApplyMapper businessApplyMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return businessApplyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(BusinessApply record) {
        businessApplyMapper.insertSelective(record);
        return record.getId();
    }

    @Override
    public BusinessApply selectByPrimaryKey(Integer id) {
        return businessApplyMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BusinessApply record) {
        return businessApplyMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ListResult<BusinessApply> page(BusinessApply record) {
        List<BusinessApply> list = businessApplyMapper.queryByPage(record);
        ListResult listResult = new ListResult(true, list);
        listResult.setPage(new Pagination(record.getPage(), businessApplyMapper.count(record), record.getSize()));
        return listResult;
    }


    /**
     * 返回新创建ID
     *
     * @param personId 申请用户ID
     * @param targetId 目标ID
     * @param reason   申请原因
     * @param type1
     * @param remark   变更内容
     * @return
     */
    @Override
    public int log(Integer personId, Integer targetId, String reason, String type1, String remark, String changeContent) {
        BusinessApply applyFromEnd = new BusinessApply();
        applyFromEnd.setApplierId(String.valueOf(personId));
        applyFromEnd.setTargetId(String.valueOf(targetId));
        applyFromEnd.setStatus(0);//状态，0申请未处理，1审核通过，2审核未通过
        applyFromEnd.setReason(reason);
        applyFromEnd.setType1(type1); // 申请类型
        applyFromEnd.setRemark(remark);
        applyFromEnd.setContent(changeContent); // 变更内容

        businessApplyMapper.insertSelective(applyFromEnd);
        return applyFromEnd.getId();
    }

    @Override
    public int check(Integer id, Integer checkId, String checkReason, Integer status) {
        BusinessApply applyFromEnd = new BusinessApply();
        applyFromEnd.setId(id);
        applyFromEnd.setCheckerId(String.valueOf(checkId));
        applyFromEnd.setCheckAt(new Date());
        applyFromEnd.setStatus(status);//状态，0申请未处理，1审核通过，2审核未通过
        applyFromEnd.setCheckReason(checkReason);

        return businessApplyMapper.updateByPrimaryKeySelective(applyFromEnd);
    }


}
